home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap05 / IconView / IconView.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  6.2 KB  |  227 lines

  1. //***********************************************************************
  2. //
  3. //  IconView.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "IconView.h"
  9.  
  10. #define IDC_LISTBOX 100
  11.  
  12. CMyApp myApp;
  13.  
  14. /////////////////////////////////////////////////////////////////////////
  15. // CMyApp member functions
  16.  
  17. BOOL CMyApp::InitInstance ()
  18. {
  19.     m_pMainWnd = new CMainWindow;
  20.     m_pMainWnd->ShowWindow (m_nCmdShow);
  21.     m_pMainWnd->UpdateWindow ();
  22.     return TRUE;
  23. }
  24.  
  25. /////////////////////////////////////////////////////////////////////////
  26. // CMainWindow message map and member functions
  27.  
  28. BEGIN_MESSAGE_MAP (CMainWindow, CWnd)
  29.     ON_WM_CREATE ()
  30.     ON_WM_PAINT ()
  31.     ON_WM_SETFOCUS ()
  32.     ON_WM_DROPFILES ()
  33.     ON_LBN_SELCHANGE (IDC_LISTBOX, OnSelChange)
  34. END_MESSAGE_MAP ()
  35.  
  36. CMainWindow::CMainWindow ()
  37. {
  38.     CString strWndClass = AfxRegisterWndClass (
  39.         0,
  40.         myApp.LoadStandardCursor (IDC_ARROW),
  41.         (HBRUSH) (COLOR_3DFACE + 1),
  42.         myApp.LoadStandardIcon (IDI_APPLICATION));
  43.  
  44.     CreateEx (0, strWndClass, "IconView",
  45.         WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
  46.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  47.         NULL, NULL, NULL);
  48.  
  49.     CRect rect (0, 0, m_cxChar * 84, m_cyChar * 21);
  50.     CalcWindowRect (&rect);
  51.  
  52.     SetWindowPos (NULL, 0, 0, rect.Width (), rect.Height (),
  53.         SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
  54. }
  55.  
  56. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  57. {
  58.     if (CWnd::OnCreate (lpcs) == -1)
  59.         return -1;
  60.  
  61.     CClientDC dc (this);
  62.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  63.  
  64.     m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  65.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  66.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  67.  
  68.     CFont* pOldFont = dc.SelectObject (&m_font);
  69.     TEXTMETRIC tm;
  70.     dc.GetTextMetrics (&tm);
  71.     m_cxChar = tm.tmAveCharWidth;
  72.     m_cyChar = tm.tmHeight + tm.tmExternalLeading;
  73.     dc.SelectObject (pOldFont);
  74.  
  75.     m_rcImage.SetRect (m_cxChar * 4, m_cyChar * 3, m_cxChar * 46,
  76.         m_cyChar * 19);
  77.  
  78.     m_ctlGroupBox.Create ("Detail",  WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
  79.         CRect (m_cxChar * 2, m_cyChar, m_cxChar * 48, m_cyChar * 20),
  80.         this, (UINT) -1);
  81.  
  82.     m_ctlLabel.Create ("Icons", WS_CHILD | WS_VISIBLE | SS_LEFT,
  83.         CRect (m_cxChar * 50, m_cyChar, m_cxChar * 82, m_cyChar * 2),
  84.         this);
  85.  
  86.     m_ctlIconListBox.Create (WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  87.         WS_BORDER | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
  88.         CRect (m_cxChar * 50, m_cyChar * 2, m_cxChar * 82, m_cyChar * 20),
  89.         this, IDC_LISTBOX);
  90.  
  91.     m_ctlGroupBox.SetFont (&m_font);
  92.     m_ctlLabel.SetFont (&m_font);
  93.  
  94.     DragAcceptFiles ();
  95.     return 0;
  96. }
  97.  
  98. void CMainWindow::PostNcDestroy ()
  99. {
  100.     delete this;
  101. }
  102.  
  103. void CMainWindow::OnPaint ()
  104. {
  105.     CPaintDC dc (this);
  106.     m_ctlIconListBox.ProjectImage (&dc, m_rcImage,
  107.         ::GetSysColor (COLOR_3DFACE));
  108. }
  109.  
  110. void CMainWindow::OnSetFocus (CWnd* pWnd)
  111. {
  112.     m_ctlIconListBox.SetFocus ();
  113. }
  114.  
  115. void CMainWindow::OnDropFiles (HDROP hDropInfo)
  116. {
  117.     int nCount = ::DragQueryFile (hDropInfo, (UINT) -1, NULL, 0);
  118.  
  119.     if (nCount == 1) { // One file at a time, please
  120.         m_ctlIconListBox.ResetContent ();
  121.  
  122.         char szFile[MAX_PATH];
  123.         ::DragQueryFile (hDropInfo, 0, szFile, sizeof (szFile));
  124.         int nIcons = (int) ::ExtractIcon (NULL, szFile, (UINT) -1);
  125.  
  126.         if (nIcons) {
  127.             HICON hIcon;
  128.             for (int i=0; i<nIcons; i++) {
  129.                 hIcon = ::ExtractIcon (AfxGetInstanceHandle (),
  130.                     szFile, i);
  131.                 m_ctlIconListBox.AddIcon (hIcon);
  132.             }
  133.         }
  134.  
  135.         CString strWndTitle = szFile;
  136.         strWndTitle += " - IconView";
  137.         SetWindowText (strWndTitle);
  138.  
  139.         CClientDC dc (this);
  140.         m_ctlIconListBox.SetCurSel (0);
  141.         m_ctlIconListBox.ProjectImage (&dc, m_rcImage,
  142.             ::GetSysColor (COLOR_3DFACE));
  143.     }
  144.     ::DragFinish (hDropInfo);
  145. }
  146.  
  147. void CMainWindow::OnSelChange ()
  148. {
  149.     CClientDC dc (this);
  150.     m_ctlIconListBox.ProjectImage (&dc, m_rcImage,
  151.         ::GetSysColor (COLOR_3DFACE));
  152. }
  153.  
  154. /////////////////////////////////////////////////////////////////////////
  155. // CIconListBox member functions
  156.  
  157. BOOL CIconListBox::PreCreateWindow (CREATESTRUCT& cs)
  158. {
  159.     if (!CListBox::PreCreateWindow (cs))
  160.         return FALSE;
  161.  
  162.     cs.dwExStyle |= WS_EX_CLIENTEDGE;
  163.     cs.style &= ~(LBS_OWNERDRAWVARIABLE | LBS_SORT);
  164.     cs.style |= LBS_OWNERDRAWFIXED;
  165.     return TRUE;
  166. }
  167.  
  168. void CIconListBox::MeasureItem (LPMEASUREITEMSTRUCT lpmis)
  169. {
  170.     lpmis->itemHeight = 36;
  171. }
  172.  
  173. void CIconListBox::DrawItem (LPDRAWITEMSTRUCT lpdis)
  174. {
  175.     CDC dc;
  176.     dc.Attach (lpdis->hDC);
  177.     CRect rect = lpdis->rcItem;
  178.     int nIndex = lpdis->itemID;
  179.  
  180.     CBrush* pBrush = new CBrush;
  181.     pBrush->CreateSolidBrush (::GetSysColor ((lpdis->itemState &
  182.         ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_WINDOW));
  183.     dc.FillRect (rect, pBrush);
  184.     delete pBrush;
  185.  
  186.     if (lpdis->itemState & ODS_FOCUS)
  187.         dc.DrawFocusRect (rect);
  188.  
  189.     if (nIndex != (UINT) -1)
  190.         dc.DrawIcon (rect.left + 4, rect.top + 2,
  191.             (HICON) GetItemData (nIndex));
  192.  
  193.     dc.Detach ();
  194. }
  195.  
  196. int CIconListBox::AddIcon (HICON hIcon)
  197. {
  198.     int nIndex = AddString ("");
  199.     if ((nIndex != LB_ERR) && (nIndex != LB_ERRSPACE))
  200.         SetItemData (nIndex, (DWORD) hIcon);
  201.     return nIndex;
  202. }
  203.  
  204. void CIconListBox::ProjectImage (CDC* pDC, LPRECT pRect,
  205.     COLORREF crBkColor)
  206. {
  207.     CDC dcMem;
  208.     dcMem.CreateCompatibleDC (pDC);
  209.  
  210.     CBitmap bitmap;
  211.     bitmap.CreateCompatibleBitmap (pDC, 32, 32);
  212.     CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
  213.  
  214.     CBrush* pBrush = new CBrush (crBkColor);
  215.     dcMem.FillRect (CRect (0, 0, 32, 32), pBrush);
  216.     delete pBrush;
  217.  
  218.     int nIndex = GetCurSel ();
  219.     if (nIndex != LB_ERR)
  220.         dcMem.DrawIcon (0, 0, (HICON) GetItemData (nIndex));
  221.  
  222.     pDC->StretchBlt (pRect->left, pRect->top, pRect->right - pRect->left,
  223.         pRect->bottom - pRect->top, &dcMem, 0, 0, 32, 32, SRCCOPY);
  224.  
  225.     dcMem.SelectObject (pOldBitmap);
  226. }
  227.